<?php
/**
 * 
 * @author libai.tc <libai.tc@gmail.com>
 */

class productModel
{
	private $_db;

	public function __construct() {
		$this->_db = new DB();
	}

	/*从数据库中获取产品
	 *参数说明:$ca 产品的分类id
	 *$start   开始的行号
	 *$step	   取多少行数据
	 */
	public function getProduct($start, $step=28, $condition, $order){
		$db=$this->_db;
		if($start<=1) 
			$start=1;
		$start=($start-1)*$step;
		
		$where_str='';
		$order_str='';
		if(!empty($condition))
			$where_str=' where ' . $condition;
		if(!empty($order))
			$order_str=' ' . $order . ' ';
		
		$count=$db->get_all("select count(*) from product $where_str $order_str");

		$sql="select * from product $where_str $order_str limit $start,$step";
		$result=$db->get_all($sql);
		if(empty($result))
			return array('total'=>0, 'rows'=>array());
		else
			return array('total'=>$count[0]['count(*)'], 'rows'=>$result);
	}
	
	public function saveProduct(){
		$name=Base::getRequest('name');
		$product_link=Base::getRequest('product_link');
		$pic_link=Base::getRequest('pic_link');
		$desc=Base::getRequest('desc');
		$click_counts=Base::getRequest('click_counts');
		$love_counts=Base::getRequest('love_counts');
		$category=Base::getRequest('category');
		$discount=Base::getRequest('discount');
		$price=Base::getRequest('price');
		$prime_price=Base::getRequest('prime_price');
		$status=Base::getRequest('status');

		$sql = "insert into `product` (`name`,`product_link`,`pic_link`,`desc`,`click_counts`,`love_counts`,`category`,`discount`,`price`,`prime_price`,`status`)
			values ('$name','$product_link','$pic_link','$desc','$click_counts','$love_counts','$category','$discount','$price','$prime_price', '$status')";
		if(!$this->_db->query($sql)){
			return json_encode(array('success'=>false));
		}
		return json_encode(array(
					'id' => mysql_insert_id(),
					'name' => $name,
					'product_link' => $product_link,
					'pic_link' => $pic_link,
					'desc' => $desc,
					'click_counts' => $click_counts,
					'love_counts' => $love_counts,
					'category' => $category,
					'discount' => $discount,
					'price' => $price,
					'prime_price' => $prime_price,
					'status'=>$status
					));
	}
	
	public function destoryProduct($id){
		$p_id=intval($id);
		$sql="delete from `product` where `id`=$p_id";
		if(!$this->_db->query($sql)){
			return json_encode(array('success'=>false));
		}
		echo json_encode(array('success'=>true));
	}
	
	public function updateProduct($id){
		$id=intval(Base::getRequest('id'));
		$name=Base::getRequest('name');
		$product_link=Base::getRequest('product_link');
		$pic_link=Base::getRequest('pic_link');
		$desc=Base::getRequest('desc');
		$click_counts=Base::getRequest('click_counts');
		$love_counts=Base::getRequest('love_counts');
		$category=Base::getRequest('category');
		$discount=Base::getRequest('discount');
		$price=Base::getRequest('price');
		$prime_price=Base::getRequest('prime_price');
		$status=Base::getRequest('status');


		$sql = "update `product` set `name`='$name',`product_link`='$product_link',`pic_link`='$pic_link',`desc`='$desc',`click_counts`='$click_counts',`love_counts`='$love_counts',
			`category`='$category',`discount`='$discount',`price`='$price',`prime_price`='$prime_price',`status`=$status where `id`=$id";

		if(!$this->_db->query($sql)){
			return json_encode(array('success'=>false));			
		}

		echo json_encode(array(
					'id' => $id,
					'name' => $name,
					'product_link' => $product_link,
					'pic_link' => $pic_link,
					'desc' => $desc,
					'click_counts' => $click_counts,
					'love_counts' => $love_counts,
					'category' => $category,
					'discount' => $discount,
					'price' => $price,
					'prime_price' => $prime_price,
					'status'=>$status
					));
	}
}

/* ------ EOF ------ */
